package com.jianying.day03;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.RestOptions;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.source.SourceFunction;

/**
 * ClassName: Flink08_Source_Self
 * Package: com.atguigu.day03
 * Description:
 *
 * @Author: tubage
 * @Create: 2024/4/1 14:24
 * @Version: 1.0
 */
public class Flink08_Source_Self {
    public static void main(String[] args) throws Exception {
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        Configuration conf = new Configuration();
        conf.set(RestOptions.PORT, 8081);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(conf);
        env.setParallelism(2);

        DataStreamSource<String> dataDS = env.addSource(new MySourceFunction());
//        env.fromSource()
        dataDS.print();
        env.execute();
    }
}

class MySourceFunction implements SourceFunction<String> {
    @Override
    public void run(SourceContext<String> ctx) throws Exception {
        // 读取数据的逻辑
        for (int i = 0; i < 100000; i++) {
            // 生成数据传递到下游
            ctx.collect("数据：" + i);
            Thread.sleep(1000);
        }
    }

    @Override
    public void cancel() {
        // job取消后执行的代码
        System.out.println("job取消");
    }
}
